<?php
namespace app\index\controller;
use app\common\controller\Api;
use think\facade\Config;
require_once __DIR__."/../../../extend/lib/TPWechat.class.php";
use lib\TPWechat;

class LogCoupon extends Api
{

    /**
     * @var \app\index\model\LogCoupon
     */
    protected $model = null;

    public $relationSearchArr = ['user', 'channel'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('LogCoupon');
    }

    //回复客服消息
    public function reply(){
        $id = $this->request->param('id');
        $type = intval($this->request->param('type'));
        $type = $type>0?$type:1;
        $content = $this->request->param('content');

        $log = $this->model->where('id', $id)->find();

        $user = model('UserInfo')->where('id', $log->user_id)->find();

        $touser = $user['wx_open_id'];

        if(!$touser){
            $this->error('该用户没有openid', null, 2);
        }

        $reply_content = [
            'touser'=>$touser
        ];

        switch ($type) {
            case 1://文本消息
                $reply_content['msgtype'] = 'text';
                $reply_content['text'] = ['content'=>$content];
                break;

            case 2://图片消息
                $reply_content['msgtype'] = 'image';
                $reply_content['image'] = ['media_id'=>$content];
                break;
            default:
                # code...
                break;
        }

        $options = Config::get('wechat_options');

        $weObj = new TPWechat($options); //创建实例对象

        $access_token = $weObj->checkAuth();

        $result = $weObj->sendCustomMessage($reply_content);

        if($result['errcode'] == 45015){
            $this->error('用户已超过48小时没和公众号互动，或已取消关注了公众号', null, 3);
        }

        $msg = [
            'channel_id'=>$log->channel_id,
            'user_id'=>$user->id,
            'wx_open_id'=>$touser,
            'create_time'=>date("Y-m-d H:i:s"),
            'type'=>$type,
            'content'=>$content
        ];
        model('LogServiceMsg')->save($msg);

        $this->success('success', $result);
    }

    public function _condition($condition){
        $filer_arr = [];
        $op_arr = [];

        $condition['channel_id'] = $this->request->param('channel_id');
        $condition['user_id'] = $this->request->param('user_id');
        $condition['source'] = $this->request->param('source');
        $condition['msg_type'] = $this->request->param('msg_type');
        $condition['reply_type'] = $this->request->param('reply_type');
        $condition['start_create_date'] = $this->request->param('start_create_date');
        $condition['end_create_date'] = $this->request->param('end_create_date');

        if($condition['channel_id']){
            $filer_arr['channel_id'] = $condition['channel_id'];
        }
        if($condition['user_id']){
            $filer_arr['user_id'] = $condition['user_id'];
        }
        if($condition['source']){
            $filer_arr['source'] = $condition['source'];
        }
        if($condition['msg_type']){
            $filer_arr['msg_type'] = $condition['msg_type'];
        }
        if($condition['reply_type']){
            $filer_arr['reply_type'] = $condition['reply_type'];
        }
        if($condition['start_create_date'] || $condition['end_create_date']){
            $filer_arr['create_time'] = $condition['start_create_date'].','.$condition['end_create_date'];
            $op_arr['create_time'] = 'BETWEEN'; 
        }
        return array($filer_arr, $op_arr);
    }
}
